package com.ljc.cjgl.controller;

import java.util.HashSet;
import java.util.List;


import org.abyss.data.specifications.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ljc.cjgl.custom.TablePageable;
import com.ljc.cjgl.custom.TableResult;
import com.ljc.cjgl.entitiy.Course;
import com.ljc.cjgl.entitiy.Experence;
import com.ljc.cjgl.form.ExperenceForm;
import com.ljc.cjgl.security.SysUser;
import com.ljc.cjgl.security.UserUtils;
import com.ljc.cjgl.service.CourseService;
import com.ljc.cjgl.service.ExperenceService;

@Controller
@RequestMapping(value="/experence")
public class ExperenceController {
	@Autowired
	private ExperenceService service;
	@Autowired
	private CourseService courseService;
	@Autowired
	private UserUtils userUtils;
	
	private Specification<Experence> buildSpec(ExperenceForm form){
		HashSet<Rule> rules=new HashSet<>();
		String search = form.getSearch();
		if(StringUtils.hasLength(search)) {
			HashSet<Rule> rules2=new HashSet<>();
			rules2.add(Rule.property("title").contain(search));
			rules2.add(Rule.property("creator").contain(search));
			rules.add(Rule.or(rules2));
		}
		return Rule.and(rules).toSpecification();
	}

	@RequestMapping(value="/manage")
	public String manage(ModelMap map) {
		List<Course> courses = courseService.findAll();
		map.put("courses", courses);
		return "experence/manage";
	}
	
	@RequestMapping("/page")
	@ResponseBody
	public Object page(TablePageable pageable,ExperenceForm form) {
		PageRequest pageRequest = pageable.bulidPageRequest();
		Specification<Experence> spec = buildSpec(form);
		Page<Experence> page = service.findAll(spec, pageRequest);
		return TableResult.bulidPageResult(page);
	}
	
	@RequestMapping(value="/edit")
	public String edit(Integer id,Integer courseId,ModelMap map) {
		Experence model=new Experence();
		if(id!=null) {
			model=service.findById(id);
		}
		List<Course> courses = courseService.findAll();
		if(courseId!=null) {
			courses.clear();
			courses.add(courseService.findById(courseId));
		}
		map.put("courses",courses);
		map.put("model", model);
		return "experence/edit";
	}
	
	@RequestMapping(value="/save")
	@ResponseBody
	public Object save(ExperenceForm form) {
		SysUser user=userUtils.getUser();
		String name=user.getName();
		if(service.save(form,name)) {
			return TableResult.bulidResult(true, "数据保存成功");
		}else {
			return TableResult.bulidResult(false, "数据保存失败");
		}
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object delete(@RequestBody List<Experence> experences) {
		if(service.delete(experences)) {
			return TableResult.bulidResult(true, "数据删除成功");
		}else {
			return TableResult.bulidResult(false, "数据删除失败");
		}
	}
	

	
}
